<template>
  <div class="container">
    <!-- 侧边栏：功能导航 -->
    <div class="sidebar">
      <ul>
        <li :class="{ active: activeTab === 'timeline' }" @click="activeTab = 'timeline'">时间线</li>
        <li :class="{ active: activeTab === 'characters' }" @click="activeTab = 'characters'">人物管理</li>
        <li :class="{ active: activeTab === 'outline' }" @click="activeTab = 'outline'">大纲归纳</li>
        <li :class="{ active: activeTab === 'snippets' }" @click="activeTab = 'snippets'">语句片段</li>
        <li :class="{ active: activeTab === 'templates' }" @click="activeTab = 'templates'">人物模板</li>
      </ul>
    </div>

    <!-- 主内容区 -->
    <div class="main-content">
      <!-- 时间线管理 -->
      <div v-if="activeTab === 'timeline'" class="tab-content">
        <h2>时间线管理</h2>
        <ul class="timeline">
          <li v-for="(event, index) in timelineEvents" :key="index">
            <strong>{{ event.timestamp }}</strong>: {{ event.description }}
          </li>
        </ul>
        <input v-model="newEvent" placeholder="新增事件" />
        <button @click="addEvent">添加事件</button>
      </div>

      <!-- 人物管理 -->
      <div v-if="activeTab === 'characters'" class="tab-content">
        <h2>人物管理</h2>
        <table>
          <thead>
            <tr>
              <th>姓名</th>
              <th>特征</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="(character, index) in characters" :key="index">
              <td>{{ character.name }}</td>
              <td>{{ character.traits }}</td>
              <td>
                <button @click="editCharacter(character)">编辑</button>
                <button @click="deleteCharacter(character)">删除</button>
              </td>
            </tr>
          </tbody>
        </table>
        <button @click="addCharacter">新增人物</button>
      </div>

      <!-- 大纲归纳 -->
      <div v-if="activeTab === 'outline'" class="tab-content">
        <h2>大纲归纳</h2>
        <textarea v-model="outline" placeholder="输入故事大纲"></textarea>
      </div>

      <!-- 语句片段 -->
      <div v-if="activeTab === 'snippets'" class="tab-content">
        <h2>语句片段</h2>
        <textarea v-model="newSnippet" placeholder="输入新的语句片段"></textarea>
        <button @click="addSnippet">保存片段</button>
        <ul>
          <li v-for="(snippet, index) in snippets" :key="index">
            {{ snippet }}
            <button @click="useSnippet(snippet)">使用</button>
          </li>
        </ul>
      </div>

      <!-- 人物模板 -->
      <div v-if="activeTab === 'templates'" class="tab-content">
        <h2>人物模板</h2>
        <select v-model="selectedTemplate">
          <option v-for="template in characterTemplates" :key="template.name" :value="template">
            {{ template.name }}
          </option>
        </select>
        <button @click="applyTemplate">应用模板</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';

// 当前激活的选项卡
const activeTab = ref('timeline');

// 时间线数据
const timelineEvents = ref([
  { timestamp: '2023-10-01', description: '故事开始' },
  { timestamp: '2023-10-05', description: '主角遇到危机' },
]);
const newEvent = ref('');
const addEvent = () => {
  if (newEvent.value) {
    timelineEvents.value.push({ timestamp: new Date().toLocaleDateString(), description: newEvent.value });
    newEvent.value = '';
  }
};

// 人物管理数据
const characters = ref([
  { name: '张三', traits: '勇敢、善良' },
  { name: '李四', traits: '狡猾、机智' },
]);
const addCharacter = () => {
  characters.value.push({ name: '新人物', traits: '未知' });
};
const editCharacter = (character) => {
  // 编辑人物逻辑
  console.log('编辑人物:', character);
};
const deleteCharacter = (character) => {
  characters.value = characters.value.filter((c) => c !== character);
};

// 大纲数据
const outline = ref('');

// 语句片段数据
const snippets = ref(['“你好，世界！”', '“这是一个测试片段。”']);
const newSnippet = ref('');
const addSnippet = () => {
  if (newSnippet.value) {
    snippets.value.push(newSnippet.value);
    newSnippet.value = '';
  }
};
const useSnippet = (snippet) => {
  outline.value += snippet + '\n';
};

// 人物模板数据
const characterTemplates = ref([
  { name: '英雄', traits: '勇敢、正义感强' },
  { name: '反派', traits: '狡猾、野心勃勃' },
]);
const selectedTemplate = ref(null);
const applyTemplate = () => {
  if (selectedTemplate.value) {
    characters.value.push({ name: '新人物', traits: selectedTemplate.value.traits });
  }
};
</script>

<style>
/* 全局样式 */
body {
  margin: 0;
  font-family: Arial, sans-serif;
}

/* 容器布局 */
.container {
  display: flex;
  height: 100vh;
}

/* 侧边栏样式 */
.sidebar {
  width: 200px;
  background-color: #f5f5f5;
  padding: 20px;
}

.sidebar ul {
  list-style: none;
  padding: 0;
}

.sidebar li {
  padding: 10px;
  cursor: pointer;
}

.sidebar li.active {
  background-color: #ddd;
}

/* 主内容区样式 */
.main-content {
  flex: 1;
  padding: 20px;
}

/* 选项卡内容样式 */
.tab-content {
  max-width: 800px;
  margin: 0 auto;
}

/* 时间线样式 */
.timeline {
  list-style: none;
  padding: 0;
}

.timeline li {
  margin-bottom: 10px;
}

/* 表格样式 */
table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20px;
}

table th,
table td {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: left;
}

table th {
  background-color: #f5f5f5;
}

/* 按钮样式 */
button {
  padding: 8px 16px;
  margin-right: 10px;
  cursor: pointer;
}

/* 输入框样式 */
input,
textarea,
select {
  width: 100%;
  padding: 8px;
  margin-bottom: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

textarea {
  height: 150px;
}
</style>